﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.LotterySrv;
using SLLottery.Common;
using System.Windows.Media.Imaging;

namespace SLLottery.LotteryView.SSC.RX
{
    public partial class SSCRX : UserControl
    {
        private LotterySrv.LotteryServiceClient srv;
        public SSCRX()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.GetPlayTypeCompleted += new EventHandler<GetPlayTypeCompletedEventArgs>(srv_GetPlayTypeCompleted);
            srv.GetPlayTypeRadioCompleted += new EventHandler<GetPlayTypeRadioCompletedEventArgs>(srv_GetPlayTypeRadioCompleted);
            srv.GetPlayTypeNumCompleted += new EventHandler<GetPlayTypeNumCompletedEventArgs>(srv_GetPlayTypeNumCompleted);
            this.Loaded += new RoutedEventHandler(SSCRX_Loaded);
        }
        void srv_GetPlayTypeCompleted(object sender, GetPlayTypeCompletedEventArgs e)
        {
            App.Session[Constant.SSCRXPLAYTYPE_SESSION] = e.Result.ToList();
            if (!App.Session.ContainsKey(Constant.PLAYTYPERADIO_SESSION))
            {
                srv.GetPlayTypeRadioAsync();
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                InitPage();
            }
        }
        void srv_GetPlayTypeRadioCompleted(object sender, GetPlayTypeRadioCompletedEventArgs e)
        {
            App.Session[Constant.PLAYTYPERADIO_SESSION] = e.Result.ToList();
            if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                InitPage();
            }
        }
        void srv_GetPlayTypeNumCompleted(object sender, GetPlayTypeNumCompletedEventArgs e)
        {
            App.Session[Constant.PLAYTYPENUM_SESSION] = e.Result.ToList();
            InitPage();
        }
        void SSCRX_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.Session.ContainsKey(Constant.PLAYTYPE_SESSION)
                || App.Session[Constant.PLAYTYPE_SESSION] == null)
            {
                srv.GetPlayTypeAsync((int)LotteryType.SSCRX);
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPERADIO_SESSION))
            {
                srv.GetPlayTypeRadioAsync();
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                List<PlayTypeInfo> list = App.Session[Constant.PLAYTYPE_SESSION] as List<PlayTypeInfo>;
                list = list.Where(p => p.LotteryTypeID1 == (int)LotteryType.SSCRX).ToList();
                App.Session[Constant.SSCRXPLAYTYPE_SESSION] = list;
                InitPage();
            }
        }
        private void InitPage()
        {
            List<PlayTypeInfo> list = App.Session[Constant.SSCRXPLAYTYPE_SESSION] as List<PlayTypeInfo>;
            if (list == null || list.Count == 0)
            {
                Messager.Show("任选模块暂时故障!");
                App.Current.RootVisual = new LoginPage();
                return;
            }
            foreach (PlayTypeInfo l in list)
            {
                Controls.PlayTypeButton btn = new Controls.PlayTypeButton();
                btn.Text = l.PlayTypeName;
                btn.Tag = (PlayType)l.PlayTypeID;
                btn.MouseLeftButtonDown += new MouseButtonEventHandler(PlayTypeButton_MouseLeftButtonDown);
                buttonPanel.Children.Add(btn);
                if (list.First() == l)
                    PlayTypeButton_MouseLeftButtonDown(btn, null);
            }
        }
        private void PlayTypeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Controls.PlayTypeButton Btn = (Controls.PlayTypeButton)sender;
            Border border = Btn.border;
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Btn_Bg_Org.png", UriKind.Relative));
            border.Background = imageBrush;
            foreach (Controls.PlayTypeButton btn in buttonPanel.Children)
            {
                if (!Btn.Equals(btn))
                {
                    Border b = btn.border;
                    imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Btn_Bg_Black.png", UriKind.Relative));
                    b.Background = imageBrush;
                }
            }
            Redirect(Btn);
        }
        public void Redirect(Controls.PlayTypeButton btn)
        {
            MainPage.dicWs.Clear();
            if (XuanHaoPanel.Children.Count > 0)
            {
                XuanHaoPanel.Children.Clear();
            }
            Controls.TouZhuCtrl touzhu = new Controls.TouZhuCtrl();
            MainPage.TouzhuControl = touzhu;
            MainPage.playType = Convert.ToInt32(btn.Tag);
            DockPanel dockPanel = new DockPanel();
            dockPanel.LastChildFill = true;
            dockPanel.Width = 658;

            Grid grid = new Grid();
            grid.RowDefinitions.Insert(0, new RowDefinition() { Height = new GridLength() });
            grid.RowDefinitions.Insert(1, new RowDefinition() { Height = new GridLength(25) });

            Controls.NumPanel numPanel = new Controls.NumPanel();
            Controls.RadioButtonPanel rbPanel = new Controls.RadioButtonPanel();
            grid.Children.Add(numPanel);
            grid.Children.Add(rbPanel);
            numPanel.SetValue(Grid.RowProperty, 0);
            rbPanel.SetValue(Grid.RowProperty, 1);

            touzhu.lotteryType = LotteryType.SSCRX;
            touzhu.PlayType = Convert.ToInt32(btn.Tag);
            touzhu.numPanel = numPanel;
            touzhu.rbPanel = rbPanel;
            touzhu.PlayTypeName = btn.Text;
            //添加按钮
            touzhu.btnAdd.Click += (s, e) =>
            {
                string num = numPanel.GetChoosedNum((SSCRXPlayType)btn.Tag).TrimEnd('$');
                if (num.Length == 0)
                {
                    Messager.Show("号码不完整,无法添加");
                    return;
                }
                if (num.StartsWith("@"))
                {
                    Messager.Show(num.Replace("@", ""));
                    return;
                }
                string[] nums = num.Split('$');
                foreach (string n in nums)
                {
                    touzhu.NumBasket.Items.Add(n);
                    ListBoxItem item = (ListBoxItem)(touzhu.NumBasket.ItemContainerGenerator.ContainerFromItem(
                        touzhu.NumBasket.Items[touzhu.NumBasket.Items.Count - 1]));
                    switch ((PlayType)btn.Tag)
                    {
                        case PlayType._R3Zhix:
                            item.Width = 280;
                            break;
                        case PlayType._R3Zux:
                            item.Width = 100;
                            break;
                        case PlayType._R2Zhix:
                            item.Width = 185;
                            break;
                        case PlayType._R2Zux:
                            item.Width = 100;
                            break;
                        case PlayType._Rydw:
                            item.Width = 455;
                            break;
                        case PlayType._Rbdw:
                            item.Width = 95;
                            break;
                    }
                    if (numPanel.isWriten)
                        item.Width = 60;
                }
                numPanel.ClearAllNums();
                touzhu.lbBetCount.Text = BetCountCalc.GetBetCount
                    (touzhu.NumBasket, (PlayType)btn.Tag, numPanel.isWriten,
                     ((PlayTypeRadio)Convert.ToInt32(rbPanel.GetChoosedRadioButton().Tag.ToString())).ToString()
                    ).ToString();
            };

            DockPanel.SetDock(grid, Dock.Top);
            dockPanel.Children.Add(grid);
            DockPanel.SetDock(touzhu, Dock.Top);
            dockPanel.Children.Add(touzhu);
            XuanHaoPanel.Children.Add(dockPanel);
            List<PlayTypeRadioInfo> list_radio = App.Session[Constant.PLAYTYPERADIO_SESSION] as List<PlayTypeRadioInfo>;
            list_radio = (from l in list_radio where l.PlayTypeID1 == (int)((PlayType)btn.Tag) select l).ToList();
            List<PlayTypeNumInfo> list_num = App.Session[Constant.PLAYTYPENUM_SESSION] as List<PlayTypeNumInfo>;
            list_num = (from l in list_num where l.PlayTypeID1 == (int)((PlayType)btn.Tag) select l).ToList();
            string numPanelText = string.Empty;
            string rbPanelText = string.Empty;
            string rbPanelValue = string.Empty;
            foreach (PlayTypeRadioInfo p in list_radio)
            {
                if (p == list_radio.First())
                    MainPage.playTypeRadio = Convert.ToInt32(p.PlayTypeRadioID);
                rbPanelText += p.PlayTypeRadioName;
                rbPanelValue += p.PlayTypeRadioID.ToString();
                if (p != list_radio.Last())
                {
                    rbPanelValue += ",";
                    rbPanelText += ",";
                }
            }
            foreach (PlayTypeNumInfo p in list_num)
            {
                numPanelText += p.PlayTypeNumName;
                if (p != list_num.Last())
                    numPanelText += ",";
            }
            numPanel.Text = numPanelText;
            rbPanel.Text = rbPanelText;
            rbPanel.Value = rbPanelValue;
            rbPanel.CreateElement();
            numPanel.CreateElement();
            foreach (RadioButton rb in rbPanel.PlayTypePanel.Children)
            {
                rb.Checked += (s, er) =>
                {
                    touzhu.NumBasket.Items.Clear();
                    numPanel.ClearChild();
                    RadioButton radioButton = (RadioButton)s;
                    string rbValue = ((PlayTypeRadio)Convert.ToInt32(radioButton.Tag.ToString())).ToString();
                    MainPage.playTypeRadio = Convert.ToInt32(radioButton.Tag.ToString());
                    if (rbValue.IndexOf("单式") > -1
                        || rbValue.IndexOf("混合") > -1)
                    {
                        numPanel.isWriten = true;
                    }
                    else
                        numPanel.isWriten = false;
                    numPanel.isCreated = false;
                    numPanel.CreateElement();
                };
            }
        }
    }
}
